<?php

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

namespace EasyActionPHP\Controller;

use EasyActionPHP\Dispatcher as Dispatcher;

/**
 * HttpResponse - Representa as respostas ao Controlador do Modelo MVC.
 *
 * @author Flavio de Vasconcellos Correa
 * @since 2007/06/04
 * @package EasyActionPHP
 * @subpackage Controller
 * @version $id$
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 */
class HttpResponse {

    /**
     * @staticvar HttpRequest singleton instancia desta classe.
     */
    static $instance;

    /**
     * @var Array Buffer de respostas enviadas.
     */
    var $attribs;

    /**
     * Construtor.
     */
    function __construct() {
        $this->attribs = Array();
    }

    /**
     * Retorna a instancia singleton desta classe.
     *
     * @access public
     * @access public
     * @return HttpResponse
     */
    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new HttpResponse();
        }
        return self::$instance;
    }

    /**
     * Registra um valor a uma resposta.
     *
     * @access public
     * @param String name
     * @param Mixed value
     * @return void
     */
    public static function setAttribute($name, $value) {
        if (isset(self::$instance)) {
            self::$instance->attribs[$name] = $value;
        } else {
            $this->attribs[$name] = $value;
        }
    }

    /**
     * Retorna um valor associado a uma resposta.
     *
     * @access public
     * @param String name
     * @return Mixed
     */
    public static function getAttribute($name) {
        if (isset(self::$instance)) {
            return self::$instance->attribs[$name];
        } else {
            return $this->attribs[$name];
        }
    }

    /**
     * Retorna a lista com todas as respostas registradas.
     *
     * @access public
     * @return Array[Mixed]
     */
    public static function getAttributes() {
        if (isset(self::$instance)) {
            return self::$instance->attribs;
        } else {
            return $this->attribs;
        }
    }

    /**
     * Executa o redirecionamento para uma URL de modo imperativo.
     *
     * @param String
     * @access public
     * @return void
     */
    public static function sendRedirect($url) {
        Dispatcher\ActionDispatcher::redirect($url);
    }

}

?>